<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="评估名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入评估名称"
            clearable style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板" prop="tempId">
        <el-select v-model="queryParams.tempId" placeholder="请选择模板" style="width: 240px">
          <el-option
              v-for="item in tempList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评估人员" prop="evaluateId">
        <el-select v-model="queryParams.evaluateId" placeholder="请选择评估人员" style="width: 240px">
          <el-option
              v-for="item in evaluateList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="warning"-->
      <!--            plain-->
      <!--            icon="Download"-->
      <!--            @click="exportReport"-->
      <!--        >导出能力等级评估报告-->
      <!--        </el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="assessmentList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" width="60"/>
      <el-table-column label="编号" align="center" prop="no" :show-overflow-tooltip="true"/>
      <el-table-column label="姓名" align="center" prop="elderName" :show-overflow-tooltip="true"/>
      <el-table-column label="性别" align="center" prop="sex" :show-overflow-tooltip="true">
        <template #default="scope">
          <div v-if="scope.row.sex == 0">男</div>
          <div v-if="scope.row.sex == 1">女</div>
          <div v-if="scope.row.sex == 2">未知</div>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" :show-overflow-tooltip="true"/>
      <el-table-column label="得分" align="center" prop="score" :show-overflow-tooltip="true"/>
      <el-table-column label="能力等级" align="center" prop="abilityLevel" :show-overflow-tooltip="true"/>
      <el-table-column label="评估员" align="center" prop="evaluator" :show-overflow-tooltip="true"/>
      <el-table-column label="评估时间" align="center" prop="updateTime" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status == 1">未完成</el-tag>
          <el-tag v-if="scope.row.status == 2" type="success">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评估完成时间" align="center" prop="completeTime" width="180"/>
      <!--      <el-table-column label="评估地点" align="center" prop="evaluateLocation" :show-overflow-tooltip="true"/>-->
      <!--      <el-table-column label="评估人员" align="center" prop="elderName" :show-overflow-tooltip="true"/>-->
      <!--      <el-table-column label="评估人员签名" align="center" prop="evaluateSign" width="120">-->
      <!--        <template #default="scope">-->
      <!--          <image-preview :src="scope.row.evaluateSign" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="评估人员签名时间" align="center" prop="evaluateSignTime" width="150"/>-->
      <!--      <el-table-column label="信息提供者签名" align="center" prop="provSign" width="130">-->
      <!--        <template #default="scope">-->
      <!--          <image-preview :src="scope.row.provSign" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="信息提供者签名时间" align="center" prop="provSignTime" width="150"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button
              type="text"
              icon="View"
              @click="handleUpdate(scope.row)"
          >查看
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改评估对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="assessmentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="评估名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入评估名称"/>
        </el-form-item>
        <el-form-item label="模板id" prop="tempId">
          <el-input v-model="form.tempId" placeholder="请输入模板id"/>
        </el-form-item>
        <el-form-item label="计划完成时间" prop="planendTime">
          <el-date-picker clearable
                          v-model="form.planendTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择计划完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号" prop="elderlyIdno">
          <el-input v-model="form.elderlyIdno" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="归档日期" prop="archiveTime">
          <el-date-picker clearable
                          v-model="form.archiveTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择归档日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评估结果" prop="result">
          <el-input v-model="form.result" placeholder="请输入评估结果"/>
        </el-form-item>
        <el-form-item label="评估人员id" prop="evaluateId">
          <el-input v-model="form.evaluateId" placeholder="请输入评估人员id"/>
        </el-form-item>
        <el-form-item label="评估人员签名" prop="evaluateSign">
          <el-input v-model="form.evaluateSign" placeholder="请输入评估人员签名"/>
        </el-form-item>
        <el-form-item label="协助人员姓名" prop="assistName">
          <el-input v-model="form.assistName" placeholder="请输入协助人员姓名"/>
        </el-form-item>
        <el-form-item label="协助人员签名" prop="assistSign">
          <el-input v-model="form.assistSign" placeholder="请输入协助人员签名"/>
        </el-form-item>
        <el-form-item label="协助人员签名时间" prop="assistSignTime">
          <el-date-picker clearable
                          v-model="form.assistSignTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择协助人员签名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="信息提供者姓名" prop="provName">
          <el-input v-model="form.provName" placeholder="请输入信息提供者姓名"/>
        </el-form-item>
        <el-form-item label="信息提供者签名" prop="provSign">
          <el-input v-model="form.provSign" placeholder="请输入信息提供者签名"/>
        </el-form-item>
        <el-form-item label="信息提供者签名时间" prop="provSignTime">
          <el-date-picker clearable
                          v-model="form.provSignTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择信息提供者签名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="老人身份证前" prop="idPre">
          <el-input v-model="form.idPre" placeholder="请输入老人身份证前"/>
        </el-form-item>
        <el-form-item label="老人身份证后" prop="idBack">
          <el-input v-model="form.idBack" placeholder="请输入老人身份证后"/>
        </el-form-item>
        <el-form-item label="残疾证" prop="disabilityCertificateImg">
          <el-input v-model="form.disabilityCertificateImg" placeholder="请输入残疾证"/>
        </el-form-item>
        <el-form-item label="户口本" prop="householdRegisterImg">
          <el-input v-model="form.householdRegisterImg" placeholder="请输入户口本"/>
        </el-form-item>
        <el-form-item label="合影" prop="sitePhotos">
          <el-input v-model="form.sitePhotos" placeholder="请输入合影"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Assessment">
import {
  listAssessment,
  getAssessment,
  delAssessment,
  addAssessment,
  updateAssessment, exportToWord
} from "@/views/pg/evaluate/api/assessment";
import {listAllAssessmentTemp} from "@/views/pg/evaluate/api/assessmentTemp.js";
import {listAllElderly} from "@/views/pg/serviceTarget/api/elderly.js";
import router from "@/router/index.js";

const {proxy} = getCurrentInstance();

const assessmentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const tempList = ref([]);
const evaluateList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    no: null,
    name: null,
    tempId: null,
    planendTime: null,
    elderlyIdno: null,
    status: null,
    archiveTime: null,
    result: null,
    evaluateId: null,
    evaluateSign: null,
    assistName: null,
    assistSign: null,
    assistSignTime: null,
    provName: null,
    provSign: null,
    provSignTime: null,
    idPre: null,
    idBack: null,
    disabilityCertificateImg: null,
    householdRegisterImg: null,
    sitePhotos: null,
    remark: null,
  },
  rules: {
    no: [
      {required: true, message: "编号不能为空", trigger: "blur"}
    ],
    name: [
      {required: true, message: "评估名称不能为空", trigger: "blur"}
    ],
    planendTime: [
      {required: true, message: "计划完成时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

onMounted(() => {
  console.log("pagePath:assessment")
})

getList();
getTempList();
getEvaluateList();

/** 查询评估列表 */
function getList() {
  loading.value = true;
  listAssessment(queryParams.value).then(response => {
    assessmentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getTempList() {
  listAllAssessmentTemp().then(response => {
    tempList.value = response.data
  });
}

function getEvaluateList() {
  listAllElderly().then(response => {
    evaluateList.value = response.data
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    no: null,
    name: null,
    tempId: null,
    planendTime: null,
    elderlyIdno: null,
    status: "0",
    archiveTime: null,
    result: null,
    evaluateId: null,
    evaluateSign: null,
    assistName: null,
    assistSign: null,
    assistSignTime: null,
    provName: null,
    provSign: null,
    provSignTime: null,
    idPre: null,
    idBack: null,
    disabilityCertificateImg: null,
    householdRegisterImg: null,
    sitePhotos: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    deleteStatus: "1"
  };
  proxy.resetForm("assessmentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评估";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // reset();
  // const _id = row.id || ids.value
  // getAssessment(_id).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改评估";
  // });
  router.push({path: '/evaluate/answer', query: {id: row.id}})
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["assessmentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAssessment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAssessment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除？').then(function () {
    return delAssessment(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

function exportReport() {
  if (ids.value && ids.value > 0) queryParams.value.ids = ids.value;
  exportToWord(queryParams.value).then(res => {
  })
}

/** 导出按钮操作 */
function handleExport() {
  if (ids.value && ids.value > 0) queryParams.value.ids = ids.value;
  proxy.download('evaluate/assessment/export', {
    ...queryParams.value
  }, `自理能力评估报告.xlsx`)
}
</script>
